package com.sky.common.basic;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.common.function.ClassField;
import com.sky.common.utils.FunctionUtil;

import java.io.Serializable;
import java.util.List;
import java.util.Map;


public class BaseEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 当前用户id
     **/
    private Integer currentUid;

    /**
     * 当前登录账户
     **/
    private String currentLoginName;

    /**
     * 当前用户CMP真实姓名
     **/
    private String currentRealName;

    /**
     * 当前C端用户昵称
     */
    private String currentNickName;

    /**
     * 操作人名称(通用) 当一个功能同时在CMP/COP/C端都能执行操作时，操作人记录可能来源不同的用户体系
     * CMP: 取 realName
     * COP: 取 userName
     * C:   取 nickName
     */
    private String commonModifyName;

    /**
     * 当前COP的用户名称
     */
    private String currentUserName;

    /**
     * 当前运营商id(只在cdw-operators-api和cdw-elephant-api服务中使用)
     **/
    private Integer currentOperatorId;

    /**
     * 当前登录账号归属的一级运营商id(只在cdw-operators-api和cdw-elephant-api服务中使用)
     **/
    private Integer currentTopOperatorId;

    /**
     * 用户来源：COP CMP C
     */
    private String userFrom;

    /**
     * 登录ip
     **/
    private String loginIp;

    /**
     * 当前页
     **/
    private Integer pageNum;

    /**
     * 每页的数量
     **/
    private Integer pageSize;

    /**
     * 分页标识 避免前端不是分页接口也传分页数据 , 导致缓存匹配错误
     **/
    private String pageFlag;
    
    /** 数据源选择(非必填, 当前只针对订单导出在使用) */
    private String dataSourceKey;
    
    public String getDataSourceKey() {
        return dataSourceKey;
    }
    
    public void setDataSourceKey(String dataSourceKey) {
        this.dataSourceKey = dataSourceKey;
    }

    public <E> Page<E> enablePage() {
        if (pageNum == null || pageNum < 1) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize < 1) {
            pageSize = 15;
        }
        if (pageSize > 1000) {
            pageSize = 1000;
        }
        pageFlag = pageNum + pageSize.toString();
        return PageHelper.startPage(pageNum, pageSize);
    }

    protected <T> Map<String, Object> filterMap(Map<String, Object> map, List<ClassField<T>> fns) {
        return FunctionUtil.filterMap(map, fns);
    }


    public Integer getPageNum() {
        return pageNum;
    }

    public void setPageNum(Integer pageNum) {
        this.pageNum = pageNum;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getCurrentUid() {
        return currentUid;
    }

    public void setCurrentUid(Integer currentUid) {
        this.currentUid = currentUid;
    }

    public String getCurrentLoginName() {
        return currentLoginName;
    }

    public void setCurrentLoginName(String currentLoginName) {
        this.currentLoginName = currentLoginName;
    }

    public String getCurrentRealName() {
        return currentRealName;
    }

    public void setCurrentRealName(String currentRealName) {
        this.currentRealName = currentRealName;
    }

    public Integer getCurrentOperatorId() {
        return currentOperatorId;
    }

    public void setCurrentOperatorId(Integer currentOperatorId) {
        this.currentOperatorId = currentOperatorId;
    }

    public String getPageFlag() {
        return pageFlag;
    }

    public void setPageFlag(String pageFlag) {
        this.pageFlag = pageFlag;
    }

    public String getLoginIp() {
        return loginIp;
    }

    public void setLoginIp(String loginIp) {
        this.loginIp = loginIp;
    }

    public Integer getCurrentTopOperatorId() {
        return currentTopOperatorId;
    }

    public void setCurrentTopOperatorId(Integer currentTopOperatorId) {
        this.currentTopOperatorId = currentTopOperatorId;
    }

    public String getUserFrom() {
        return userFrom;
    }

    public void setUserFrom(String userFrom) {
        this.userFrom = userFrom;
    }

    public boolean isCOP() {
        return !"CMP".equals(userFrom);
    }

    public String getCurrentNickName() {
        return currentNickName;
    }

    public void setCurrentNickName(String currentNickName) {
        this.currentNickName = currentNickName;
    }

    public String getCurrentUserName() {
        return currentUserName;
    }

    public void setCurrentUserName(String currentUserName) {
        this.currentUserName = currentUserName;
    }

    /**
     * 获取操作人名称(通用)
     * 适用于一个功能同时在CMP/COP/C端都能执行操作时，操作人记录可能来源不同的用户体系 此时名称需要区分
     */
    public String getCommonModifyName() {
        if ("CMP".equals(userFrom)) {
            return currentRealName + "(CMP)";
        } else if ("COP".equals(userFrom)) {
            return currentUserName + "(COP)";
        } else if ("C".equals(userFrom)) {
            return currentNickName + "(C)";
        }
        return currentLoginName;
    }

}
